﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BrokerBase.Lib.Person;
using NHibernate;
using Telerik.Windows.Diagrams.Core;
using Telerik.Windows.Controls.Diagrams.Extensions.ViewModels;

namespace Broker.View.PersonView
{
    /// <summary>
    /// PersonRelationMapForm.xaml 的交互逻辑
    /// </summary>
    public partial class PersonRelationMapPage
    {
        private IList<IPerson> persons;
        private PersonGraphSource personGraphSource = new PersonGraphSource();

        public PersonRelationMapPage()
        {
            InitializeComponent();

            Loaded += new RoutedEventHandler(PersonRelationMapPage_Loaded);
        }

        void PersonRelationMapPage_Loaded(object sender, RoutedEventArgs e)
        {
            this.drmRelation.GraphSource = personGraphSource;
            LoadPersons();
            ViewPersonShap(persons);

            AddLink();

            //DiagramViewModel model = new DiagramViewModel();

            //this.drmRelation.GraphSource = model;

        }

        private void TemplateChangeButtonClick(object sender, RoutedEventArgs e)
        {

        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            var settings = new SugiyamaSettings
            {
                // you can use the vertical direction though it'd result in a less conventional apperance
                Orientation = Telerik.Windows.Diagrams.Core.Orientation.Horizontal,

                // the horizontal distance or separation between item on the same layer
                HorizontalDistance = 60d,
                VerticalDistance = 60d,
            };

            drmRelation.Layout(Telerik.Windows.Diagrams.Core.LayoutType.Sugiyama, settings);

            drmRelation.AutoFit();

            var links = personGraphSource.Links;
            foreach (ILink l in links)
            {
                PersonGraphicViewModel p1 = l.Source as PersonGraphicViewModel;
                PersonGraphicViewModel p2 = l.Target as PersonGraphicViewModel;

            }
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {

        }

        private void LoadPersons()
        {
            using (ISession session = LibGlobal.HbmSessionFactory.OpenSession())
            {
                persons = session.CreateQuery("from Person").List<IPerson>();
            }
        }

        private void AddLink()
        {
            IEnumerable<PersonGraphicViewModel> items = personGraphSource.Items as IEnumerable<PersonGraphicViewModel>;


            PersonGraphicViewModel p1 = items.FirstOrDefault(p => p.Name == "李如江");
            PersonGraphicViewModel p2 = items.FirstOrDefault(p => p.Name == "owen");

            PersonLinkViewModel pvm = new PersonLinkViewModel(p1, p2) { TargetCapType = CapType.Arrow1Filled };

            pvm.Name = "朋友";
            pvm.Visibility = Visibility.Visible;
            //pvm.Content = "OK";
            pvm.Visibility = Visibility.Visible;
            personGraphSource.AddLink(pvm);

            


        }


        private void ViewPersonShap(IList<IPerson> persons)
        {
            foreach (IPerson p in persons)
            {
                PersonGraphicViewModel pvm = new PersonGraphicViewModel(p);
                pvm.Width = 150;
                pvm.Height = 90;

                BrushConverter brushConverter = new BrushConverter();

                pvm.Background = (Brush)brushConverter.ConvertFromString("#D0D0D0");

                personGraphSource.AddNode(pvm);
            }
        }
    }
}

public class DiagramViewModel : GraphSourceBase<NodeViewModelBase, LinkViewModelBase<NodeViewModelBase>>
{
    public DiagramViewModel()
    {
        var first = new NodeViewModelBase
        {
            Content = "First item",
            Position = new Point(50, 100)
        };
        var second = new NodeViewModelBase
        {
            Content = "Second item",
            Position = new Point(150, 100),
            RotationAngle = 45
        };
        var third = new NodeViewModelBase
        {
            Content = "Third item",
            Position = new Point(250, 100)
        };
        this.AddNode(first);
        this.AddNode(second);
        this.AddNode(third);
        this.AddLink(new LinkViewModelBase<NodeViewModelBase>(first, second));
        this.AddLink(new LinkViewModelBase<NodeViewModelBase>(second, third));
    }
}